﻿using System;
using System.IO;
using System.Globalization;
using System.Linq;
using System.Windows;

using Core.Localization;
using Core.Logic;
using Core.Logic.Options;
using UI.Properties;

namespace UI
{
    public partial class App
    {    
        #region Public fields

        public ModelManager ModelManager { get { return _modelManager; } }

        public OptionsManager OptionsManager { get { return _optionsManager; } }

		public LocalizationManager LocalizationManager { get { return _localizationManager; } }

        public SerializationManager SerializationManager { get { return _serializationManager.Value; } }

		public PdfExportManager PdfExportManager { get { return _pdfExportManager.Value; } }

        public DependencyContainer DependencyContainer
        {
            get { return _dependencyContainer; }
        }

        #endregion

		#region Private fields

		private ModelManager _modelManager;

		private readonly OptionsManager _optionsManager = new OptionsManager();

		private readonly LocalizationManager _localizationManager = new LocalizationManager();

		private readonly DependencyContainer _dependencyContainer = new DependencyContainer();

        private readonly Lazy<SerializationManager> _serializationManager = new Lazy<SerializationManager>();

		private readonly Lazy<PdfExportManager> _pdfExportManager = new Lazy<PdfExportManager>();

        #endregion 

        #region Event handlers

        private void ApplicationStartup(object sender, StartupEventArgs e)
        {
			_modelManager = new ModelManager(_localizationManager);

	        _optionsManager.OptionsChanged += OptionsManagerOptionsChanged;

			string optionsPath = String.Format(@"{0}{1}", AppDomain.CurrentDomain.BaseDirectory, Settings.Default.OptionsFilePath);
            try
            {
				_optionsManager.LoadOptions(optionsPath);
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("Your settings file is corrupted or can`t be found. Default settings would be installed.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

				_optionsManager.UpdateOptions(Options.CreateDefaultOptions(_localizationManager));
				_optionsManager.SaveOptions(optionsPath);
            }

            if (e.Args.Any())
            {
                DependencyContainer.Project = SerializationManager.LoadProject(e.Args[0]);
            }
        }

	    private void OptionsManagerOptionsChanged(object sender, OptionsManager.OptionsChangedEventArgs e)
	    {
			if (e.OldOptions == null || e.NewOptions.Language != e.OldOptions.Language)
			{
				_localizationManager.SetLocale(CultureInfo.GetCultureInfo(e.NewOptions.Language.GetStringValue()));

				e.NewOptions.Keywords = e.NewOptions.Keywords.Select(keyword => _localizationManager.Localize(keyword)).ToList();
			}
		    if (e.OldOptions == null || e.NewOptions.ModelsCatalog != e.OldOptions.ModelsCatalog)
		    {
			    ModelManager.Load(e.NewOptions.ModelsCatalog);
		    }
	    }

	    #endregion
    }
}